package com.zhouyu.gamecommunity.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhouyu.gamecommunity.config.WebSocketServer;
import com.zhouyu.gamecommunity.entity.Game;
import com.zhouyu.gamecommunity.entity.PageData;
import com.zhouyu.gamecommunity.entity.Result;
import com.zhouyu.gamecommunity.service.CommonService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CommonController {

    @Resource
    private CommonService commonService;

    @PutMapping("/attention/{userId}/{gameId}")
    public Result<Integer> attentionGame(@PathVariable("userId") String userId, @PathVariable("gameId") String gameId) {
        return commonService.attentionGame(userId, gameId);
    }

    @DeleteMapping("/attention/{userId}/{gameId}")
    public Result<Integer> deleteAttention(@PathVariable("userId") String userId, @PathVariable("gameId") String gameId) {
        return commonService.deleteAttention(userId, gameId);
    }

    @PostMapping("/getComment")
    private Result<PageData<JSONObject>> getGameComment(@RequestBody JSONObject params, HttpServletRequest request) {
        String id = request.getHeader("Authorization");
        params.put("id", id);
        return commonService.getGameComment(params);
    }

    @PostMapping("/countComment")
    private Result<Integer> countComment(@RequestBody JSONObject params) {
        return commonService.countComment(params);
    }

    @PostMapping("/comment/addComment")
    private Result<Integer> addComment(@RequestBody JSONObject params) {
        return commonService.addComment(params);
    }

    @PostMapping("/article/addArticle")
    private Result<Integer> addArticle(@RequestBody JSONObject params) {
        return commonService.addArticle(params);
    }

    @PostMapping("/article/getArticle")
    private Result<List<JSONObject>> getArticle(@RequestBody JSONObject params) {
        return commonService.getArticle(params);
    }

    @GetMapping("/article/content/{id}")
    private Result<List<JSONObject>> getArticleContent(@PathVariable("id") String id) {
        return commonService.getArticleContent(id);
    }

    @GetMapping("/article/banner/{id}")
    private Result<List<JSONObject>> getArticleBanner(@PathVariable("id") String id) {
        return commonService.getArticleBanner(id);
    }

    @GetMapping("/attention/{id}")
    private Result<List<JSONObject>> getUserAttentionList(@PathVariable("id") String id) {
        return commonService.getUserAttentionList(id);
    }

    @PostMapping("/getTopicList")
    private Result<List<JSONObject>> getTopicList(@RequestBody JSONObject params) {
        return commonService.getTopicList(params);
    }

    @PutMapping("/subscribe/{userId}/{topicId}")
    public Result<Integer> subscribeTopic(@PathVariable("userId") String userId, @PathVariable("topicId") String topicId) {
        return commonService.subscribeTopic(userId, topicId);
    }

    @DeleteMapping("/subscribe/{userId}/{topicId}")
    public Result<Integer> deleteSubscribe(@PathVariable("userId") String userId, @PathVariable("topicId") String topicId) {
        return commonService.deleteSubscribe(userId, topicId);
    }

    @PostMapping("/like")
    public Result<Integer> likeObject(@RequestBody JSONObject params) {
        return commonService.likeObject(params);
    }

    @PostMapping("/game/search")
    public Result<List<Game>> searchGame(@RequestBody JSONObject params) {
        return commonService.searchGame(params);
    }

    @PostMapping("/getUp")
    public Result<List<JSONObject>> getUpList(@RequestBody JSONObject params, HttpServletRequest request) {
        String id = request.getHeader("Authorization");
        params.put("userId", id);
        return commonService.getUpList(params);
    }

    @PostMapping("/attentionUser")
    public Result<Integer> attentionUser(@RequestBody JSONObject params, HttpServletRequest request) {
        String id = request.getHeader("Authorization");
        params.put("id", id);
        return commonService.attentionUser(params);
    }

    @GetMapping("/sendSocketMsg/{msg}")
    public Result<Integer> sendSocketMsg(@PathVariable("msg") String msg) {
        try {
            WebSocketServer.sendInfo(msg, "123");
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.success();
    }
}
